/*
 * Copyright (c) Created by T C L on 2019.7.9.
 */

package cn.mumzone.pos.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.MoneyFormatUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.R;
import cn.mumzone.pos.event.TimesCardReturnRealMoneyEvent;
import cn.mumzone.pos.ui.widgets.keyboard.PosKeyBoardView;

public class TimesCardReturnMoneyInputDialogFragment extends DialogFragment {

    @BindView(R.id.tv_return_amount)
    TextView tvReturnAmount;
    @BindView(R.id.tv_real_return_amount)
    EditText tvRealReturnAmount;
    @BindView(R.id.keyboard_view)
    PosKeyBoardView keyboardView;

    private String money;

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(false);//点击外部不消失
        // 设置SelectPicPopupWindow弹出窗体的背景
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        params.gravity = Gravity.CENTER;
        params.width = DisplayUtil.dip2px(getActivity(), 438);
        params.height = DisplayUtil.dip2px(getActivity(), 490);
        window.setAttributes(params);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        money = getArguments().getString("money");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_times_card_return_money_input, container, false);
        ButterKnife.bind(this, view);
        tvReturnAmount.setText("应退金额 ¥" + money);
        keyboardView.attachTo(tvRealReturnAmount);
        MoneyFormatUtil.setTextFilter(tvRealReturnAmount);
        keyboardView.setOnConfirmClickListener(new PosKeyBoardView.OnConfirmClickListener() {
            @Override
            public void onConfirmClick(String text) {
                String formattedMoney = MoneyFormatUtil.getFormattedMoney(text, true);
                if (!TextUtils.isEmpty(formattedMoney)) {
                    double realReturnMoney = Double.parseDouble(text);
                    if (realReturnMoney > Double.parseDouble(money)) {
                        PosToast.INSTANCE.makeToast(getActivity(), PosToast.ToastType.TOAST_TYPE_RECTANGLE,
                                PosToast.ToastRectangleLevel.ALERT_WARNING_LEVEL_ONE, "实退金额不能大于账单总额~", false);
                        return;
                    } else {
                        RxBus.getDefault().post(new TimesCardReturnRealMoneyEvent(formattedMoney));
                    }
                }
                dismiss();
            }
        });
        return view;
    }

    @OnClick(value = {R.id.iv_close})
    public void toggleEvent(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
        }
    }
}
